package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	ServiceSystem "github.com/flipped-aurora/gin-vue-admin/server/service/system"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SysPatientsApi struct{}

// CreateSysPatients 创建患者
// @Tags SysPatients
// @Summary 创建患者
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPatients true "创建患者"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /sysPatients/createSysPatients [post]
func (sysPatientsApi *SysPatientsApi) CreateSysPatients(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var sysPatients system.SysPatients
	err := c.ShouldBindJSON(&sysPatients)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	store_id, err := ServiceSystem.UserServiceApp.GetStoreIdById(utils.GetUserID(c))
	if err != nil {
		global.GVA_LOG.Error("获取门店id失败!", zap.Error(err))
		response.FailWithMessage("获取门店id失败:"+err.Error(), c)
		return
	}
	//StoreId
	sysPatients.StoreId = int(store_id)

	err = sysPatientsService.CreateSysPatients(ctx, &sysPatients)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSysPatients 删除患者
// @Tags SysPatients
// @Summary 删除患者
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPatients true "删除患者"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /sysPatients/deleteSysPatients [delete]
func (sysPatientsApi *SysPatientsApi) DeleteSysPatients(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := sysPatientsService.DeleteSysPatients(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSysPatientsByIds 批量删除患者
// @Tags SysPatients
// @Summary 批量删除患者
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /sysPatients/deleteSysPatientsByIds [delete]
func (sysPatientsApi *SysPatientsApi) DeleteSysPatientsByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := sysPatientsService.DeleteSysPatientsByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSysPatients 更新患者
// @Tags SysPatients
// @Summary 更新患者
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.SysPatients true "更新患者"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /sysPatients/updateSysPatients [put]
func (sysPatientsApi *SysPatientsApi) UpdateSysPatients(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var sysPatients system.SysPatients
	err := c.ShouldBindJSON(&sysPatients)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = sysPatientsService.UpdateSysPatients(ctx, sysPatients)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSysPatients 用id查询患者
// @Tags SysPatients
// @Summary 用id查询患者
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询患者"
// @Success 200 {object} response.Response{data=system.SysPatients,msg=string} "查询成功"
// @Router /sysPatients/findSysPatients [get]
func (sysPatientsApi *SysPatientsApi) FindSysPatients(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	resysPatients, err := sysPatientsService.GetSysPatients(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(resysPatients, c)
}

// GetSysPatientsList 分页获取患者列表
// @Tags SysPatients
// @Summary 分页获取患者列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.SysPatientsSearch true "分页获取患者列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /sysPatients/getSysPatientsList [get]
func (sysPatientsApi *SysPatientsApi) GetSysPatientsList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.SysPatientsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	store_id, err := ServiceSystem.UserServiceApp.GetStoreIdById(utils.GetUserID(c))
	if err != nil {
		global.GVA_LOG.Error("获取门店id失败!", zap.Error(err))
		response.FailWithMessage("获取门店id失败:"+err.Error(), c)
		return
	}
	//StoreId
	StoreId := int(store_id)

	list, total, err := sysPatientsService.GetSysPatientsInfoList(ctx, pageInfo, StoreId)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSysPatientsPublic 不需要鉴权的患者接口
// @Tags SysPatients
// @Summary 不需要鉴权的患者接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /sysPatients/getSysPatientsPublic [get]
func (sysPatientsApi *SysPatientsApi) GetSysPatientsPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	sysPatientsService.GetSysPatientsPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的患者接口信息",
	}, "获取成功", c)
}
